<?php

namespace app\modules\frontend\controllers;

use app\models\Audio;
use app\models\Collect;
use app\models\Comment;
use app\models\CommentPraise;
use app\models\Label;
use app\models\Praise;
use app\models\User;
use app\lib\DataMessage;
use app\modules\frontend\LoginFilter;
use Yii;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;
use yii\web\NotFoundHttpException;

class AudioController extends BaseController {
    public function behaviors() {
        return [
            'access' => [
                'class' => LoginFilter::className(),
                'only'=> ['comment', 'collect', 'praise'],
                'rules' => [
                    [
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'comment' => ['post'],
                    'collect' => ['post'],
                    'praise' => ['post'],
                ],
            ],
        ];
    }

    public function actionIndex($id) {
        // 查询Audio咯~ findOne 相当于 Audio::find()->one();，就是获取一个的意思，首页哪里all是多个的意思
        $audio = Audio::findOne($id);
        $user = Yii::$app->user->identity;
        if(!$audio) {
            // 如果audio 不存在，就报个错，比如弄个404
            // 当然啦，现在页面很简陋，因为错误页还没好好改呢
            throw new NotFoundHttpException('该音频不存在~');
        }

        // 统计阅读数
        $audio->read_times ++;
        $audio->save();

        /** @var User $user */
        $user = Yii::$app->user->identity;
        $ifCollect = $user && Collect::find()->where([
            'user_id'=>$user->id,
            'audio_id'=>$audio->id,
        ])->exists();
        $ifPraise = $user && Praise::find()->where([
            'user_id'=>$user->id,
            'audio_id'=>$audio->id,
        ])->exists();

        return $this->render('index', [
            'audio' => $audio,
            'user' => $user,
            'ifCollect' => $ifCollect,
            'ifPraise' => $ifPraise,
        ]);
    }

    public function actionLabel() {
        $labels = Label::find()->where([])->all();
        $labelsGroup = ArrayHelper::map($labels, 'id', function($v) { return $v; }, 'class');
        // var_dump($labelsGroup);exit;

        return $this->render('label', [
            'labelsGroup' => $labelsGroup,
        ]);
    }

    // 发布评论
    public function actionComment() {
        /** @var User $user */
        $user = Yii::$app->user->identity;
        $context = Yii::$app->request->post('context');
        $audioId = Yii::$app->request->post('audio_id');
        $audio = Audio::findOne($audioId);
        if(!$audio){
            throw new NotFoundHttpException('该音频不存在~');
        }

        if(empty($context)) {
            return DataMessage::initFail(1, '评论未填写');
        }
        $comment = new Comment();
        $comment->audio_id = $audioId;
        $comment->context = $context;
        $comment->user_id = $user->id;
        if(!$comment->save()){
            return DataMessage::initFail(1, $comment->errors);
        }
        $audio->comment_num ++;
        $audio->save();
        return DataMessage::initSuccess();
    }

    // 收藏
    public function actionCollect() {
        /** @var User $user */
        $user = Yii::$app->user->identity;
        $ifCollect = Yii::$app->request->post('if-collect');
        $audioId = Yii::$app->request->post('audio-id');
        $audio = Audio::findOne($audioId);
        if(!$audio){
            throw new NotFoundHttpException('该音频不存在~');
        }

        $collect = Collect::findOne([
            'user_id'=>$user->id,
            'audio_id'=>$audioId,
        ]);
        if($ifCollect && !$collect) {
            $collect = new Collect();
            $collect->user_id=$user->id;
            $collect->audio_id = $audioId;
            $collect->save();
        }else if(!$ifCollect && $collect) {
            $collect->delete();
        }
        return DataMessage::initSuccess();
    }

    // 点赞
    // TODO 点赞和收藏有啥区别。。代码都一模一样，操作逻辑都一模一样。
    public function actionPraise() {
        /** @var User $user */
        $user = Yii::$app->user->identity;
        $ifPraise = Yii::$app->request->post('if-praise');
        $audioId = Yii::$app->request->post('audio-id');
        $audio = Audio::findOne($audioId);
        if(!$audio){
            throw new NotFoundHttpException('该音频不存在~');
        }

        $praise = Praise::findOne([
            'user_id'=>$user->id,
            'audio_id'=>$audioId,
        ]);
        if($ifPraise && !$praise) {
            $praise = new Praise();
            $praise->user_id=$user->id;
            $praise->audio_id = $audioId;
            $praise->save();
            $audio->praise_num ++;
            $audio->save();
        }else if(!$ifPraise && $praise) {
            $praise->delete();
            $audio->praise_num --;
            $audio->save();
        }
        return DataMessage::initSuccess();
    }

    // 评论点赞
    public function actionCommentPraise() {
        /** @var User $user */
        $user = Yii::$app->user->identity;
        $ifPraise = Yii::$app->request->post('if-praise');
        $commentId = Yii::$app->request->post('comment-id');
        $comment = Comment::findOne($commentId);
        if(!$comment){
            throw new NotFoundHttpException('该音频不存在~');
        }

        $praise = CommentPraise::findOne([
            'user_id'=>$user->id,
            'comment_id'=>$commentId,
        ]);
        if($ifPraise && !$praise) {
            $praise = new CommentPraise();
            $praise->user_id=$user->id;
            $praise->comment_id = $commentId;
            $praise->save();
            $comment->praise_num ++;
            $comment->save();
        } else if(!$ifPraise && $praise) {
            $praise->delete();
            $comment->praise_num --;
            $comment->save();
        }
        return DataMessage::initSuccess($comment->praise_num);
    }
}
